package com.example.receiptocr.ocr

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import com.example.receiptocr.ui.theme.ReceiptTheme
import dagger.hilt.android.AndroidEntryPoint
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import com.example.receiptocr.MainActivity

@AndroidEntryPoint
class OCRResultActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val uri = intent.getStringExtra("photo_uri")?.let { Uri.parse(it) }
        setContent {
            ReceiptTheme {
                if (uri != null) {
                    Surface(
                        modifier = Modifier.fillMaxSize(),
                        color = MaterialTheme.colorScheme.background
                    ) {
                        OCRResultScreen(
                            receiptId = 1,
                            onNavigateBack = {
                                finish() // 普通返回
                            },
                            uri = uri,
                            onSaveAndReturn = {
                                // 创建一个新的 Intent 来启动 MainActivity 并清除任务栈
                                val intent = Intent(this, MainActivity::class.java).apply {
                                    flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
                                    putExtra("navigate_to_home", true) // 添加标记以导航到首页
                                }
                                startActivity(intent)
                                finish()
                            }
                        )
                    }
                }
            }
        }
    }
}
